package com.librarysys.service.impl;

import com.librarysys.common.Result;
import com.librarysys.dao.*;
import com.librarysys.entity.*;
import com.librarysys.service.IBorrowService;
import com.librarysys.vo.BorrowVO;
import com.librarysys.vo.ReaderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yuhui on 2017/7/13.
 */
@Service("iBorrowService")
public class BorrowServiceImpl implements IBorrowService{

    @Autowired
    private ReaderMapper readerMapper;

    @Autowired
    private ReadertypeMapper readertypeMapper;

    @Autowired
    private BookinfoMapper bookinfoMapper;

    @Autowired
    private PublishingMapper publishingMapper;

    @Autowired
    private BookcaseMapper bookcaseMapper;

    @Autowired
    private ParameterMapper parameterMapper;

    public  Result getReaderInfo(String barcode){
        if(barcode==null){
            return Result.createByError("参数错误");
        }
        Reader reader = readerMapper.selectByBarcode(barcode);
        if(reader==null){
            return Result.createByError("没有该读者信息");
        }
        String type="";
        Integer number=0;

        Readertype readertype = readertypeMapper.selectByPrimaryKey(reader.getTypeid());
        if(readertype!=null){
            type=readertype.getName();
            number=readertype.getNumber();
        }
        ReaderVO readerVO = new ReaderVO(reader.getId(), reader.getName(), reader.getSex(), reader.getBarcode(),reader.getPapertype(), reader.getPaperno(), type, number);
        return Result.createBySuccess("查询成功",readerVO);
    }

    public Result borrowList(String type,String value){
        List<BorrowVO> list=new ArrayList<BorrowVO>();
        if("barcode".equals(type)){
            List<Bookinfo> bookinfos = bookinfoMapper.selectByBarcode(value);
            list=toBorrowList(bookinfos);
        }
        if("name".equals(type)){
            value="%"+value+"%";
            List<Bookinfo> bookinfos = bookinfoMapper.selectByName(value);
            list=toBorrowList(bookinfos);
        }
        return Result.createBySuccess("查询成功",list);
    }

    private List<BorrowVO> toBorrowList(List<Bookinfo> list){
        ArrayList<BorrowVO> borrowVOS = new ArrayList<>();
        for(Bookinfo bookinfo:list){
            BorrowVO borrowVO = new BorrowVO();
            borrowVO.setId(bookinfo.getId());
            borrowVO.setName(bookinfo.getBookname());
            borrowVO.setPrice(bookinfo.getPrice());

            Bookcase bookcase = bookcaseMapper.selectByPrimaryKey(bookinfo.getBookcase());
            if(bookcase==null){
                borrowVO.setBookcase("");
            }else{
                borrowVO.setBookcase(bookcase.getName());
            }

            Publishing publishing = publishingMapper.selectBy(bookinfo.getIsbn());
            if(publishing==null){
                borrowVO.setPublishing("");
            }else{
                borrowVO.setPublishing(publishing.getPubname());
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(new Date());
            borrowVO.setBorrowTime(format);

            Parameter parameter = parameterMapper.selectBy();
            Integer validity = parameter.getValidity();
            Long back=new Date().getTime()+(validity*30 * 24 * 60 * 60 * 1000);
            Date date = new Date(back);
            String format1 = simpleDateFormat.format(date);
            borrowVO.setBackTime(format1);
            borrowVOS.add(borrowVO);
        }

        return borrowVOS;
    }
}
